package com.tgpms.web.assets.controller;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.*;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.web.assets.entity.MaterialIssues;
import com.tgpms.web.assets.entity.MaterialReceivingReport;
import com.tgpms.web.assets.service.MaterialIssuesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MaterialIssuesController
 * @Description TODO
 * @Author GUAN
 * @Date 2019-11-12 16:22
 * @Version 1.0
 * @return
 **/
@Api(value = "MaterialIssuesController", tags = "固定资产调拨")
@RestController
@RequestMapping(value = "/ev/materialIssues")
public class MaterialIssuesController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MaterialIssuesService materialIssuesService;

    @Autowired
    private TokenService tokenService;

    /**
     * @Author guan
     * @Description 查询固定资产调拨信息
     * @Date 10:14 2019-11-13
     * @Param [selparam, leftdition, condition, dataowner, pageNo, pageSize]
     * @return com.tgpms.common.Result
     **/
    @ApiOperation(value = "查询固定资产调拨信息", notes = "查询固定资产调拨信息", httpMethod = "POST")
    @PostMapping(value = "/selMaterialIssuesList")
    public Result selMaterialIssuesList(@RequestParam(value = "selparam", required = false) String selparam,
                    @RequestParam(value = "leftdition", required = false) String leftdition,
                    @RequestParam(value = "condition", required = false) String condition,
                    @RequestParam(value = "dataowner", required = false) String dataowner,
                    @RequestParam(value = "pageNo", required = true) String pageNo,
                    @RequestParam(value = "pageSize", required = true) String pageSize) {
        Result result = new Result();
        PageInfo pageInfo = new PageInfo();
        if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            pageInfo.setPage(pageNo);
            pageInfo.setSize(pageSize);
            Map<String,String> map = new HashMap<String,String>();
            map.put("selparam", selparam);
            map.put("leftdition", leftdition);
            map.put("condition", condition);
            map.put("dataowner", dataowner);
            pageInfo.setQueryMap(map);
            result = materialIssuesService.selMaterialIssuesList(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
            throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
        return result;
    }

    /**
     * @Author guan
     * @Description 添加固定资产调拨信息
     * @Date 10:16 2019-11-13
     * @Param [materialIssues, request]
     * @return com.tgpms.common.Result
     **/
    @ApiOperation(value = "添加固定资产调拨接口信息", notes = "添加固定资产调拨接口信息", httpMethod = "POST")
    @PostMapping(value = "/addMaterialIssuesInfo")
    public Result addMaterialIssuesInfo(@RequestBody MaterialIssues materialIssues, HttpServletRequest request) {
        Result result = new Result();
        try {
            //获取token操作人
            materialIssues.setInsertUser(tokenService.getUserName(request));
            materialIssues.setInsertDate(new Date());
            Map<String, String> map = materialIssuesService.addMaterialIssuesInfo(materialIssues);
            if(map.get("code").equals("1")) {
                result.setSuccess(true);
                result.setData(map.get("data"));
            }else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        return result;
    }

    /**
     * @Author guan
     * @Description 更新固定资产调拨
     * @Date 10:16 2019-11-13
     * @Param [materialIssues, request]
     * @return com.tgpms.common.Result
     **/
    @ApiOperation(value = "更新固定资产调拨接口信息", notes = "更新固定资产调拨接口信息", httpMethod = "PUT")
    @PutMapping(value = "/updateMaterialIssuesInfo")
    public Result updateMaterialIssuesInfo(@RequestBody MaterialIssues materialIssues,HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(materialIssues.getIssue())) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            //获取token操作人
            materialIssues.setModifyUser(tokenService.getUserName(request));
            materialIssues.setModifyDate(new Date());
            Map<String, String> map = materialIssuesService.updateMaterialIssuesInfo(materialIssues);
            if(map.get("code").equals("1")) {
                result.setSuccess(true);
            }else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
            throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
        }
        return result;
    }

    /**
     * @Author guan
     * @Description 删除固定资产调拨
     * @Date 10:16 2019-11-13
     * @Param [issue]
     * @return com.tgpms.common.Result
     **/
    @ApiOperation(value = "删除固定资产调拨接口信息", notes = "删除固定资产调拨接口信息", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteMaterialIssuesInfo")
    public Result deleteMaterialIssuesInfo(@RequestParam(value = "issue", required = true) String issue) {
        Result result = new Result();
        if(Common.isEmpty(issue)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            Map<String, String> map = materialIssuesService.deleteMaterialIssuesInfo(issue);
            if(map.get("code").equals("1")) {
                result.setSuccess(true);
            }else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
            throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
        }
        return result;
    }
}
